package com.learn.educourseboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learn.eduapi.entity.Course;
import com.learn.eduapi.entity.CourseActivity;
import com.learn.educourseboot.mapper.CourseActivityMapper;
import com.learn.educourseboot.mapper.CourseMapper;
import com.learn.educourseboot.service.CourseActivityService;
import com.learn.educourseboot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: edu-learn
 * @Author:
 * @CreateTime: 2020-10-16 16:16
 * @Description:
 */
@Service
public class CourseActivityServiceImpl extends ServiceImpl<CourseActivityMapper, CourseActivity> implements CourseActivityService {

    @Autowired
    private CourseActivityMapper courseActivityMapper;

    @Override
    public CourseActivity reduceActivityStock(Long activityId) {
        CourseActivity act = courseActivityMapper.selectById(activityId);
        if(act == null){
            throw new RuntimeException("此课程活动id不存在");
        }
        if(act.getStock() == 0){
            throw new RuntimeException("此课程活动库存不足");
        }
        act.setStock(act.getStock() - 1);
        courseActivityMapper.updateById(act);
        return act;
    }
}
